#! /usr/bin/env python

import sys
import cv

if __name__ == "__main__":
    if len(sys.argv) > 1:
        fn = sys.argv[1]
        img = cv.LoadImageM(fn, cv.CV_LOAD_IMAGE_GRAYSCALE)
        eig_image = cv.CreateMat(img.rows, img.cols, cv.CV_32FC1)
        temp_image = cv.CreateMat(img.rows, img.cols, cv.CV_32FC1)
        features = cv.GoodFeaturesToTrack(img, eig_image, temp_image, 5000, 0.04, 1.0, useHarris=True)
        oimg = cv.CreateImage((img.width, img.height), 8, 3)

        f = open(fn[:len(fn)-4] + '.txt','w')
        for feature in features:
            x,y = feature
            cv.Circle(oimg, (int(x),int(y)), 1, 16711680)
            f.write(str(x) + ' ' + str(y) +'\n')
        
        f.close()
        cv.SaveImage(fn[:len(fn)-4] + '_marked.png',oimg)